package com.ymatou.iapi.sellerproduct.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.AddProductInLivesCall;
import com.ymatou.iapi.sellerproduct.service.RemoveProductFromLiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.AppCountry;
import com.ymttest.database.model.YmtProductsInLive;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.AppCountryWapper;
import com.ymttest.database.sqlwapper.YmtProductBrandWapper;
import com.ymttest.database.sqlwapper.YmtProductCategoryWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_AddProductInLives {
	private static AddProductInLivesCall addProductInLivesCall = new AddProductInLivesCall();
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static Integer sellerId2 = Integer.valueOf(EnvSetup.getData("productsellerid2"));
	private static String sellerName2 = EnvSetup.getData("productsellername2");
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	private static String sellerName1 = EnvSetup.getData("productsellername4");
	public static SimpleDateFormat liveSDF = YMTDateUtil.getYMDHMSFormat();
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	private static SimpleDateFormat sdfForName = new SimpleDateFormat("MM-dd HH:mm:ss");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加商品到直播");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addProductInLivesCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}

	public static void main(String[] args) throws Exception{
		//1 create live
		JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
		Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
		
		int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
		
		List<String> productIds = new ArrayList<>();
		//2 add new product
		for (int i=0;i<10;i++){
		 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
		 String productId =	productBean.getJSONObject("Data").getString("ProductID");
		 productIds.add(productId);
		}
		
		 
		//3 add product to live
    	JSONObject addProductInLiveBean = SellerProductCallService.addProductsInLive(sellerId,liveId,productIds);
		Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_001() {
		Logger.start(true, "添加商品到直播（直播进行中 - 发布但不上架）");
		try{
			
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = new ArrayList<>();
			//2 add new product
			for (int i=0;i<10;i++){
			 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 productIds.add(productId);
			}
			
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductsInLive(sellerId,liveId,productIds);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");
			
		/*	
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");
			
			//=====================================================================================================================================			
			
			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(liveId).get(0);
			Map<String, Object> sqlTgtMap = MapUtil.hashMap("iuserid", sellerId, "sproductid", productId, "iactivityid", liveId);
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));
			

			   //check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mongo 老库检查  LiveProductInfo & LiveBasicInfo
				addProductInLivesCall.checkLiveProductInfo(liveBean,productBean,liveId,productId);
				addProductInLivesCall.checkLiveBasicInfo(liveBean,productBean,liveId);
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//Mongo 新库检查  LiveProductModel & LiveModel
				addProductInLivesCall.checkLiveProductModel(liveBean,productBean,liveId,productId);
				addProductInLivesCall.checkLiveModel(liveBean,productBean,liveId);
				
			}
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 1);
			SellerProductCallService.checkListingTime(productId, YMTDateUtil.getDate());
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
			*/
			
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_002() {
		Logger.start(true, "添加商品到直播后，再移出直播");
		try{
			
			
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");
			
			YMTDateUtil.waitTime(2);
			
			//4 remove product from live
			Map<String, Object> removeUpdateMap = new HashMap<String,Object>();
			removeUpdateMap.put("ProductId", productId);
			removeUpdateMap.put("LiveId", liveId);
			removeUpdateMap.put("UserId", sellerId);
			JSONObject removeProductBean = new RemoveProductFromLiveCall().callAndGetReturnData(removeUpdateMap);
			Logger.verifyEquals( 200,removeProductBean.getInt("Code"), "响应报文-返回码");
			
			//check SQL YmtProductsInLiveWapper
			YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
			Logger.verifyEquals(1, ymtProductsInLiveWapper.selectByLiveId(liveId).size(),"YmtProductsInLiveWapper 移出直播，记录清除");
			Logger.verifyEquals(-1, ymtProductsInLiveWapper.selectByLiveId(liveId).get(0).getiAction(),"YmtProductsInLiveWapper iaction记录置为-1");
			
			
			//Check Mongo
			//ProductBasicInfo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//不维护
				//Map<String, Object> ProductBasicInfoMap = new ProductBasicInfoWapper().getByProductId(productId);
				//Logger.verifyEquals(false,Boolean.parseBoolean(ProductBasicInfoMap.get("IsLiveProduct").toString()), "Mongo-ProductBasicInfo IsLiveProduct");
				
			
				//LiveProductInfo
				Map<String, Object> liveProductInfoMap = new LiveProductInfoWapper().getliveproductByProductId(productId);
				Logger.verifyIsNull(liveProductInfoMap, "Mongo-LiveProductInfo");
			}  
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//Mongo 新库检查  LiveProductModel & LiveModel
				Map<String, Object> liveProductModelMap = new LiveProductModelWapper().getActivityId(liveId);
				Logger.verifyIsNull(liveProductModelMap, "Mongo-LiveProductModel");
				
			}
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 1);
			SellerProductCallService.checkListingTime(productId, YMTDateUtil.getDate());
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(0, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_003() {
		Logger.start(true, "添加商品到直播后（直播未开始-商品发布但不上架），再移出直播");
		try{
						
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");
			
			YMTDateUtil.waitTime(1);
			
			//4 remove product from live
			Map<String, Object> removeUpdateMap = new HashMap<String,Object>();
			removeUpdateMap.put("ProductId", productId);
			removeUpdateMap.put("LiveId", liveId);
			removeUpdateMap.put("UserId", sellerId);
			JSONObject removeProductBean = new RemoveProductFromLiveCall().callAndGetReturnData(removeUpdateMap);
			Logger.verifyEquals( 200,removeProductBean.getInt("Code"), "响应报文-返回码");
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 4);
			SellerProductCallService.checkListingTime(productId, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(0, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_004() {
		Logger.start(true, "添加商品到直播后（直播未开始-商品已上架）");
		try{
						
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductOnSale(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");			
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 1);
			SellerProductCallService.checkListingTime(productId, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_005() {
		Logger.start(true, "添加商品到直播后（直播未开始-商品定时上架 - 直播开始在前）");
		try{
						
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductOnTime(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");			
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 3);
			SellerProductCallService.checkListingTime(productId,null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_006() {
		Logger.start(true, "添加商品到直播后（直播未开始-商品定时上架 - 直播开始在后）");
		try{
						
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveBegin6dayInFuture(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductOnTime(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");			
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 2);
			SellerProductCallService.checkListingTime(productId, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_007() {
		Logger.start(true, "添加商品到直播后（直播未开始-商品发布但不上架）");
		try{
						
			//1 create live
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			Logger.verifyEquals( 200,liveBean.getInt("Code"), "响应报文-返回码");
			
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 add new product
			 JSONObject productBean = SellerProductCallService.addNewProductOnTime(sellerId,sellerName,null);
			 String productId =	productBean.getJSONObject("Data").getString("ProductID");
			 
			//3 add product to live
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,liveId,productId);
			Logger.verifyEquals( 200,addProductInLiveBean.getInt("Code"), "响应报文-返回码");			
			
			//新品时间检查
			SellerProductCallService.checkSqlListingTime(productId, 3);
			SellerProductCallService.checkListingTime(productId, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productId), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_008() {
		Logger.start(true, "litingtime已过，添加到未开始的直播，litingtime不变");
		try{
						
			//创建一个未开始的直播
			Integer activityid = LiveCallService.createLiveBegin6dayInFuture(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个立即上架的商品
			String productid = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//call接口
			JSONObject response = SellerProductCallService.addProductInLive(sellerId,activityid, productid);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			JSONObject request = response.getJSONObject(JsonBeanHelper.JsonBeanRequest);
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 2);
			SellerProductCallService.checkListingTime(productid, null);
				
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productid), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_009() {
		Logger.start(true, "litingtime已过，添加到正在进行的直播，litingtime不变");
		try{
						
			//创建一个正在进行的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个立即上架的商品
			String productid = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//call接口
			JSONObject response = SellerProductCallService.addProductInLive(sellerId,activityid, productid);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			JSONObject request = response.getJSONObject(JsonBeanHelper.JsonBeanRequest);
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 2);
			SellerProductCallService.checkListingTime(productid, null);
				
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productid), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_010() {
		Logger.start(true, "litingtime未过，添加到未开始的直播，上架时间<直播时间,litingtime商品上架时间");
		try{
						
			//创建一个未开始的直播
			Integer activityid = LiveCallService.createLiveBegin6dayInFuture(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个5天后定时上架的商品
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//call接口
			JSONObject response = SellerProductCallService.addProductInLive(sellerId,activityid, productid);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			JSONObject request = response.getJSONObject(JsonBeanHelper.JsonBeanRequest);
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 2);
			SellerProductCallService.checkListingTime(productid, null);
				
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productid), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
			
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_011() {
		Logger.start(true, "litingtime未过，添加到未开始的直播，直播时间<上架时间,litingtime直播的开始时间");
		try{
						
			//创建一个未开始的直播
			Integer activityid = LiveCallService.createLiveBegin6dayInFuture(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个8天后定时上架的商品
			Calendar c = Calendar.getInstance();
			Map<String, Object> updateMap = new HashMap<String,Object>();
			c.add(Calendar.DAY_OF_MONTH, 8);
			updateMap.put("Product->ValidStart",sdf.format(c.getTime()));
			c.add(Calendar.DAY_OF_MONTH, 15);
			updateMap.put("Product->ValidEnd",sdf.format(c.getTime()));
			
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updateMap).getJSONObject("Data").getString("ProductID");
			//call接口
			JSONObject response = SellerProductCallService.addProductInLive(sellerId,activityid, productid);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			JSONObject request = response.getJSONObject(JsonBeanHelper.JsonBeanRequest);
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 3);
			SellerProductCallService.checkListingTime(productid, null);
				
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productid), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddProductInLives_012() {
		Logger.start(true, "litingtime未过，添加到进行中的直播，litingtime直播的开始时间");
		try{
						
			//创建一个进行中的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个5天后定时上架的商品
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//call接口
			JSONObject response = SellerProductCallService.addProductInLive(sellerId,activityid, productid);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			JSONObject request = response.getJSONObject(JsonBeanHelper.JsonBeanRequest);
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 1);
			SellerProductCallService.checkListingTime(productid, null);
				
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(1, new LiveProductsWapper().getCountByProductId(productid), "Mongo-LiveProducts 检查直播商品记录个数");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		}catch(Exception e){
			Logger.fail(e);
			
		}

	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private static void checkLiveProductModel(JSONObject liveRqBean,JSONObject productRqBean,int liveId,String productId) throws Exception{
		JSONObject liveBean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
		
		
		//category data
		YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
		
		int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
		
		Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
		String ThirdCategoryName = ret.get("sCategory").toString();
		int CategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(CategoryId).get(0);
		String CategoryName = ret.get("sCategory").toString();
		int MasterCategoryId = Integer.parseInt(ret.get("iMasterCategory").toString());
		
		ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(MasterCategoryId).get(0);
		String MasterCategoryName = ret.get("sCategory").toString();
		
		
		////////////////////////////////////////////////////////////////////////
		Map<String, Object> liveProductModelMap = new LiveProductModelWapper().getActivityId(liveId);
		
		Map<String, Object> tgtMap = new HashMap<String,Object>();
		
		tgtMap.put("ProductId", productId);
		tgtMap.put("LiveId", liveId);
		tgtMap.put("LiveName", liveBean.get("ActivityName"));
		tgtMap.put("SellerId", liveBean.get("UserId"));
		tgtMap.put("BrandId", productBean.get("iBrandId"));
		tgtMap.put("MasterCategoryId", MasterCategoryId);
		tgtMap.put("MasterCategoryName", MasterCategoryName);
		tgtMap.put("CategoryId", CategoryId);
		tgtMap.put("CategoryName", CategoryName); 
		tgtMap.put("ThirdCategoryId", ThirdCategoryId);
		tgtMap.put("ThirdCategoryName", ThirdCategoryName);
		tgtMap.put("SellStatus", 1);
		tgtMap.put("IsTop", false);
		
		AssertService.assertResultEqual(tgtMap, liveProductModelMap);
		
		
		
		//comment字段不维护
		
		
		
		
	}
	
	
	private static void checkLiveModel(JSONObject liveRqBean,JSONObject productRqBean,int liveId) throws Exception{
		
		JSONObject bean = liveRqBean.getJSONObject("JsonBeanRequest").getJSONObject("ActivityRequest");
		
		Map<String, Object> mongoData = new LiveModelWapper().getActivityId(liveId);
		
		Map<String, String> tgtAndActKeyPaths = new HashMap<String, String>();
		
		tgtAndActKeyPaths.put("ActivityName", "ActivityName");
		tgtAndActKeyPaths.put("Position", "ShopAddress");
		tgtAndActKeyPaths.put("Title", "Title");
		tgtAndActKeyPaths.put("VideoCover", "VideoCover");
		tgtAndActKeyPaths.put("VideoUrl", "VideoUrl");
		tgtAndActKeyPaths.put("UserId", "SellerId");
		tgtAndActKeyPaths.put("PicUrl", "ActivityPicture");
		tgtAndActKeyPaths.put("SellerName", "Seller");
	
		
		AssertService.assertResultEqual(tgtAndActKeyPaths, bean,mongoData);
	
		//check categroylist and brands
		Map<String, Object>  tgtMap = new HashMap<String,Object>();
		if (productRqBean==null){
			tgtMap.put("Brands", null);
			tgtMap.put("CategoryList", null);
			AssertService.assertResultEqual(tgtMap, mongoData);
		}else{
			
			JSONObject productBean = productRqBean.getJSONObject("JsonBeanRequest").getJSONObject("Product");
			JSONObject mongoDataMap = new JSONObject(mongoData);
			String brandName = new YmtProductBrandWapper().getProductBrandByBrandId(productBean.getInt("iBrandId")).get(0).get("sBrandEn").toString();
			
			YmtProductCategoryWapper ymtProductCategoryWapper = new YmtProductCategoryWapper();
			int ThirdCategoryId = Integer.parseInt(productBean.get("iThirdCategoryId").toString());
			Map<String, Object> ret = ymtProductCategoryWapper.getProductCategoryByCategoryId(ThirdCategoryId).get(0);
			String ThirdCategoryName = ret.get("sCategory").toString();
			
			Logger.verifyEquals(true, mongoDataMap.get("Brands").toString().contains(brandName), "LiveModel-Brand");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(String.valueOf(ThirdCategoryId)), "LiveModel-CategoryId");
			Logger.verifyEquals(true, mongoDataMap.get("CategoryList").toString().contains(ThirdCategoryName), "LiveModel-CategoryName");
		}
		
		
		
		
		//check country
		List<AppCountry> AppCountryList = new AppCountryWapper().selectByCountryId(bean.getInt("CountryId"));
		Logger.verifyEquals(1, AppCountryList.size(), "AppCountryList-count");
		Logger.verifyEquals(AppCountryList.get(0).getScountrynamezh(),mongoData.get("Country").toString(), "LiveModel-country");
		Logger.verifyEquals(AppCountryList.get(0).getSflag(),mongoData.get("Flag").toString(), "LiveModel-Flag");
		
		//check AppConfirm
		boolean tgtConfirm = bean.getInt("AppConfirmed")==1?true:false;
		Logger.verifyEquals(tgtConfirm, Boolean.parseBoolean(mongoData.get("AppConfirmed").toString()), "LiveModel-AppConfirmed");
		
		//check other value
		Logger.verifyEquals(1, Integer.parseInt(mongoData.get("Action").toString()), "LiveModel-Action");
		Logger.verifyEquals(liveId, Integer.parseInt(mongoData.get("ActivityId").toString()), "LiveModel-liveId");
		
		//check time
		AssertService.assertTime(YMTDateUtil.getDate(), (Date)mongoData.get("AddTime"), 1000);
		
		
		if (YMTDateUtil.getDate().getTime()>((Date)mongoData.get("StartTime")).getTime()){
			//表明正在进行直播
			Calendar c = Calendar.getInstance();
			c.add(Calendar.HOUR,-2);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("StartTime"), 1000);
			c.add(Calendar.HOUR,4);
			AssertService.assertTime(c.getTime(), (Date)mongoData.get("EndTime"), 1000);
			
		}else{
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("StartTime")), (Date)mongoData.get("StartTime"), 1000);
			AssertService.assertTime(YMTDateUtil.parseYMDHMSDate(bean.getString("EndTime")), (Date)mongoData.get("EndTime"), 1000);
		}
				
		
		
		
		
		
	}
}
	

	
	
	
	
	
	
	
	
	
	